#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int numberOfAlternatingGroups(vector<int>& colors, int k) {
        int diffrents = 1, res = 0;
        int n = colors.size();
        int left = 1, right = 0;
        while (left % n > 0 && colors[left % n] != colors[(left - 1) % n])
        {
            ++left;
            ++diffrents;
        }
        if (diffrents == n && colors[n - 1] != colors[0]) return n;

        left %= n;
        right = left + 1;
        diffrents = 1;
        while (right < left + n)
        {
            while (right < left + n && colors[right % n] != colors[(right - 1) % n])
            {
                ++right;
                ++diffrents;
            }
            int tmp = diffrents - k + 1;
            if (tmp > 0) res += tmp;

            ++right;
            diffrents = 1;
        }
        //if(diffrents + 1 == n && colors[r])
        return res;
    }
};